<?php
class Shopping_IndexController extends Zendvn_Controller_Action{
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 8,
									'pageRange' => 4,
								  );
	
	//Thong so phan trang cho comment
	protected $_pagcomment = array(
									'itemCountPerPage' => 3,
									'pageRange' => 4,
	);
	protected $_namespace;
	
	public function init(){
		
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];
		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';	

							 
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		 //Truyen tham so phan trang cho comment
		$this->_pagcomment['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['pagcomment'] = $this->_pagcomment;
		
	
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
		
		$template_path = TEMPLATE_PATH . "/public/books";
		$this->loadTemplate($template_path,'template.ini','template');
		
	}
	
	public function indexAction() {
		
		$tblProduct = new Shopping_Model_Product();
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'public-index'));
		
		/* $totalItem  = $tblProduct->countItem($this->_arrParam,array('task'=>'public-index'));
		$paginator = new Zendvn_Paginator();
 		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
		 */
	}
	public function categoryAction(){
		$tblProduct = new Shopping_Model_Product();
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'public-category'));		
		$totalItem  = $tblProduct->countItem($this->_arrParam,array('task'=>'public-category'));		
		$paginator = new Zendvn_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
        
		$tblCategory = new Shopping_Model_Category();
		$this->view->listCategory = $tblCategory->listCategory(null,array('task'=>'list-category'));
	}
	public function detailAction(){
		
		//Xu ly form comment cho san pham
		$captcha = new Zendvn_Captcha_Image();
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
		if($this->_request->isPost()){
			$captcha->removeImg($this->_arrParam['captchaID']);
			$validator = new Shopping_Form_ValidateCommentDetail($this->_arrParam);
			if($validator->isError() == true){
				$this->view->arrErrors = $validator->getMessageError();
				$this->view->Item	   = $validator->getData();
			}else{
				$tblComment = new Shopping_Model_Comment();
				$this->_arrParam['date'] = date('Y-m-d H:i:s');
				$tblComment->saveItem($this->_arrParam,array('task'=>'comment-detail'));				
			}
		}
		
		//Hien thi detail san pham
		$tblProduct = new Shopping_Model_Item();
		$this->view->Item = $tblProduct->getItem($this->_arrParam,array('task'=>'public-detail'));		
		$this->view->Title = $this->view->Item['name'] . ' | www.zend.vn';
		$this->view->headTitle($this->view->Title,true);
		
		//Hien thi cac comment cua san pham do
		$tblComment = new Shopping_Model_Comment();
		$this->view->listComment = $tblComment->listItem($this->_arrParam,array('task'=>'comment-detail'));
		//Phan trang cho cac comment
		$totalComment  = $tblComment->countItem($this->_arrParam,array('task'=>'comment-detail'));
	 	$paginator     = new Zendvn_Paginator();
		$this->view->pagcomment = $paginator->createPaginator($totalComment,$this->_pagcomment);
	}
	public function addItemAction(){
		$filter = new Zend_Filter_Digits();
		$id = $filter->filter($this->_arrParam['id']);
		
		$yourCart = new Zend_Session_Namespace('cart');
		$ssInfo = $yourCart->getIterator();
		
		if(count($yourCart->cart) == 0){ //Neu gio hang rong
			$cart[$id] =1;
			$yourCart->cart = $cart;
		}else{ //Neu gio hang da co san pham
			$tmp = $ssInfo['cart'];
			if(array_key_exists($id, $tmp) ==true){//Neu san pham da co trong gio hang
				$tmp[$id] = $tmp[$id] + 1;
			}else{ //Neu san pham chua co trong gio hang
				$tmp[$id] = 1;
			}
			$yourCart->cart = $tmp;
		}
		$this->_redirect($this->_currentController . '/view-cart');
		$this->_helper->viewRenderer->setNoRender();
	}
	public function viewCartAction(){
		$yourCart = new Zend_Session_Namespace('cart');
		if($this->_request->isPost()){
			$itemProduct = $this->_arrParam['itemProduct'];
			if(count($itemProduct)>0){
				foreach ($itemProduct as $key=>$value){
					if($value == 0){
						unset($itemProduct[$key]);
					}
				}
			}
		
			$yourCart->cart = $itemProduct;
		}
		$ssInfo = $yourCart->getIterator();
		
		$tblProduct = new Shopping_Model_Item();
		$this->_arrParam['cart'] = $ssInfo['cart'];
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'view-cart'));
		$this->view->cart =  $ssInfo['cart'];
	}
	
	public function deleteItemCartAction(){
		$yourCart = new Zend_Session_Namespace('cart');
		$ssInfo = $yourCart->getIterator();
		$tmp = $ssInfo['cart'];
		if(!empty($this->_arrParam['id'])){
			if(count($tmp)>0){
				foreach ($tmp as $key=>$value){
					if($key == $this->_arrParam['id']){
						unset($tmp[$key]);
					}
				}
			}
		}
		$yourCart->cart = $tmp;
		$this->_redirect($this->_currentController . '/view-cart');
		$this->_helper->viewRenderer->setNoRender();
	}
	public function orderAction(){
		$yourCart = new Zend_Session_Namespace('cart');
		$ssInfo = $yourCart->getIterator();
		$tblProduct = new Shopping_Model_Item();
		$this->_arrParam['cart'] = $ssInfo['cart'];
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'view-cart'));
		$this->view->cart =  $ssInfo['cart'];
		
		//Tao captcha va truyen ra view
		$captcha = new Zendvn_Captcha_Image();
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
		if($this->_request->isPost()){
			$captcha->removeImg($this->_arrParam['captchaID']);
			$validator = new Shopping_Form_ValidateOrder($this->_arrParam);
			if($validator->isError() == true){
				$this->view->arrErrors = $validator->getMessageError();
				$this->view->Item	   = $validator->getData();
			}else{
				
			}
		}
	}
}






















